package org.brisling.medical.queryService;

import java.util.List;
import java.util.Map;

import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;

import org.brisling.authorize.shiro.ShiroSecurityService;
import org.brisling.base.domain.ListDomain;
import org.brisling.base.service.AbstractBaseQueryService;
import org.brisling.common.ConstSysParam;
import org.brisling.common.util.DateUtil;
import org.brisling.medical.domain.DuplicateCharge;
import org.brisling.sysman.queryService.QueryDictionaryService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.ModelAndView;

/**
 * 
 * <p>Title: QueryDuplicatechargeService</p>
 * <p>Description: 重复收费监控service</p>
 * <p>Company: tongking</p>
 * @author trastor
 * @since 1.0
 * @version 1.0
 * @date 2016年7月20日 下午10:03:38
 */
@RestController
@RequestMapping("/duplicateCharge/query")
public class QueryDuplicatechargeService extends AbstractBaseQueryService<DuplicateCharge>{
	

	@Autowired
	private QueryDictionaryService qryDictionaryService;
	
	@Autowired
	private ShiroSecurityService shiroSecurityService;
	
	public QueryDuplicatechargeService(){
		this.moduleName = ConstSysParam.MODULE_DUPLICATE_CHARGE;
		this.subSystem = ConstSysParam.SUBSYSTEM_MEDICAL;
		this.tableName = ConstSysParam.VIEW_PREFIX +this.getTableName(DuplicateCharge.class);
		this.clazz = DuplicateCharge.class;
		typeMap = this.getTypeMap(clazz);
	}
	
	@PersistenceContext(unitName=ConstSysParam.SUBSYSTEM_SYSMAN)
	@Autowired
	protected void setEntityManager(EntityManager entityManager){
		this.entityManager = entityManager;		
	}
	
	@RequestMapping("/mainPage.action")
	public ModelAndView getMainPage(){
		return this.getBizMainPage();
	}
	
	/* (non-Javadoc)
	 * @see org.brisling.service.sysman.DepartmentServiceInf#getEditPage()
	 */
	@RequestMapping("/editPage.action")
	public ModelAndView getEditPage(){
		return this.getBizEditPage();
	}
	/**
	 * 全体都返回相同的popMain
	 * @return
	 */
	@RequestMapping("/popPage.action")
	public ModelAndView getPopPage(){
		String userid = shiroSecurityService.getCurrentUserIdentify();
		String _page = "popMain";
		return new ModelAndView("/medical/" + this.moduleName + "/"+_page);
	}
	@RequestMapping("/getListDomain.action")
	public ListDomain<DuplicateCharge> getListDomain(@RequestBody(required=false) String body){		
		
		Map<String,Object> map = this.getBodyParam(body);
		
		Integer year = null, month = null;
		String userid = shiroSecurityService.getCurrentUserIdentify();
		Map<String,String> paramMap = (Map<String, String>) map.get(ConstSysParam.CONDITION_MAP_KEY);
		if(paramMap.containsKey("monitor_year_month")){
			String yearMont = paramMap.get("monitor_year_month");
			String time[] = yearMont.split("-");
			if(time.length >= 2){
				year = Integer.valueOf(time[0]);
				month = Integer.valueOf(time[1]);
			}
		}
		if(year == null){
			return new ListDomain<>();
		}
		String yearTime[] = DateUtil.getMonthDay(year, month);
		String sql = qryDictionaryService.getSqlByBizcode("medicalPrescribeNum");
		sql = String.format(sql, yearTime[2], yearTime[0], yearTime[1]);
		
		if(!this.isSuperDept(userid,ConstSysParam.QUERY_ALL, "query_medical_all")){
			if(isManageJob(userid)){
				String deptids = this.getJobDeptidAndAccountDeptids(userid);
				paramMap.put("dept_code_in", deptids);
				sql = sql + "where %1$s ";
			}
			else{
				String deptids = this.getJobDeptidAndAccountDeptids(userid);
				paramMap.put("dept_code_in", deptids);
				sql = sql + "where %1$s ";
			}
			//查找科室数据
		}
		sql = sql + " order by prescribe_num desc";
		map.put(ConstSysParam.CONDITION_MAP_KEY, paramMap);
		//查询表
		return super.getListDomainByConditionAndOrder(sql, map, typeMap, orderMap, true);
	}
	
	@RequestMapping("/getList.action")
	public List<DuplicateCharge> getList(@RequestParam(value="page",required=false) Integer page,
			@RequestParam(value="rows",required=false) Integer rows,
			@RequestParam(value="deptid",required=false) Integer deptid,
			@RequestParam(value="sort",required=false) String sort){		
		
		StringBuilder sb = new StringBuilder();
		String sql = "";
		if(deptid!=null){
			String whereCorse=String.format(sb.append("\"deptid\" = %1$d").toString(),deptid);
			sql = String.format(this.TW_SQL, this.tableName,whereCorse);
		}else{
			sql = String.format(this.T_SQL, this.tableName);
		}
		
		return super.getPagedList(page,rows,sql);
	}
	
	/**
	 * 根据用户id查找用户信息
	 * @param id	用户id
	 * @return 用户信息记录 
	 */
	@RequestMapping("/getOne.action")
	public DuplicateCharge getOne(@RequestParam(value="id",required=false) Long id){
		return super.getOne(id);
	}
	
	/**
	 * 根据用户唯一编码查找用户信息
	 * @param identify	用户唯一编码
	 * @return 用户信息记录
	 */
	@RequestMapping("/getOneByIdentify.action")
	public DuplicateCharge getOneByIdentify(@RequestParam(value="identify",required=false) String identify){	
		return super.getOneByIdentify(identify);
	}
	
	
}
